package com.xieke.admin.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xieke.admin.dto.ResultInfo;
import com.xieke.admin.dto.UserInfo;
import com.xieke.admin.entity.Pic;
import com.xieke.admin.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import com.xieke.admin.service.IPicService;
import org.springframework.web.multipart.MultipartFile;

/**
 *  前端控制器
 *
 * @author xieke
 * @email 949118693@qq.com
 * @date 2019-08-02 11:42:22
 */
@Controller
@RequestMapping("/pic")
public class PicController {

    private static final String PATH_PIC = "";

    @Resource
    private IPicService iPicService;

    @RequestMapping("/*")
    public void toHtml(){

    }

    @RequestMapping("/listData")
    @RequiresPermissions("pic:view")
    public @ResponseBody
    ResultInfo<List<Pic>> listData(Pic pic, Integer page, Integer limit){
        QueryWrapper<Pic> wrapper = new QueryWrapper<>(pic);
        IPage<Pic> pageObj = iPicService.getBaseMapper().selectPage(new Page<>(page,limit), wrapper);
        return new ResultInfo<>(pageObj.getRecords(), (int)pageObj.getTotal());
    }

    @RequestMapping("/add")
    @RequiresPermissions("pic:add")
    public @ResponseBody
    ResultInfo<Integer> add(Pic pic, @RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request){
        return new ResultInfo<>(iPicService.savePic(file,pic));
    }

    @RequestMapping("/delBatch")
    @RequiresPermissions("pic:del")
    public @ResponseBody
    ResultInfo<Boolean> delBatch(Integer[] idArr){
        boolean b = iPicService.getBaseMapper().deleteBatchIds(Arrays.asList(idArr))>0?true:false;
        return new ResultInfo<>(b);
    }

    @RequestMapping("/edit")
    @RequiresPermissions("pic:edit")
    public @ResponseBody
    ResultInfo<Boolean> edit(Pic pic){
        boolean b = iPicService.updateById(pic);
        return new ResultInfo<>(b);
    }

}
